<template>
	<view class="content">
		<Header></Header>
		<view class="home-tabs">
			<!-- 未选中时：inactiveStyle 选中时： -->
			<u-tabs 
			:list="topBar" 
			@change="changeTabs" 
			:current='current'
			:inactiveStyle="{
				color:'#AAAAAA',
			}"
			:activeStyle="{
				fontSize:'30rpx',
				color:'#333',
				fontWeight:'bolder'
			}"
			></u-tabs>
		</view>
		<view class="home-swiper">
			<swiper 
			:duration="500" 
			current="current"
			@change="changeSwiper"
			:style="'height:' + swiperHeight + 'px'"
			>
				<swiper-item v-for="(v,i) in topBar" :key="i">
					<!-- 让其可以纵向滑动 -->
					<scroll-view scroll-y="true" :style="'height:' + swiperHeight + 'px'" @scrolltolower="loadingMore">
						<keep-alive>
							<component :is="activeCom" :courseList="courseList" v-if="i == current"></component>
						</keep-alive>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>
<script>
	import Header from "../../components/common/Header.vue"
	import Recommend from "../../components/home/recommend.vue"
	import Free from "../../components/home/free.vue"
	import Practice from "../../components/home/practice.vue"
	import { getCourse } from '../../api/course.js'
	// 引入含有动态计算高度的混入
	import { mixins } from '../../mixins/mixins.js'
	
	export default {
		mixins:[mixins],
		data () {
			return {
				// swiper高度值
				swiperHeight:0,
				current:0,
				topBar:[
					{name:'今日推荐'},
					{name:'免费课程'},
					{name:'实战课程'}
				],
				activeCom:Recommend,
				// 大家都在看课程数据
				courseList:[],
				// 课程请求参数
				courseParams:{ pageNum:1,pageSize:3,entity:{sortBy:"clicks-desc" }},
			}
		},
		onReady() {
			this.swiperHeight = this.getHeight('.header-search','.home-tabs')
		},
		onLoad() {
			// 需要在onload生命周期时，执行这个方法
			this.getAllSee(this.courseParams)
		},
		methods:{
			// 切换tabs
			changeTabs({index}) {
				this.current = index
				this.displayCom(index)
			},
			// 切换swiper
			changeSwiper(e) {
				this.current = e.detail.current
				this.displayCom(e.detail.current)
			},
			// 展示组件
			displayCom(index) {
				switch(index) {
					case 0:
					this.activeCom = Recommend;
					break;
					case 1:
					this.activeCom = Free;
					break;
					case 2:
					this.activeCom = Practice;
				}
			},
			// 获取大家都在看的数据
			getAllSee(params) {
			// 获取课程 由于接口数据需要token暂时无法获取到服务器的数据
				getCourse(params).then(res => {
					this.courseList = [...this.courseList,...res.pageInfo.list]
				})
			},
			// 滚动到底部加载更多数据
			loadingMore() {
				// 每次触底需要让参数当中pageNum+1
				this.courseParams.pageNum++
				// 当触底的时候需要再次调用请求数据的方法getAllSee
				this.getAllSee(this.courseParams)
			}
		},
		components:{
			Header,
			Recommend,
			Free,
			Practice
		}
	}
</script>
<style>
/deep/ .u-tabs__wrapper__nav__item__text {
		font-size: 29rpx;
		font-weight: 400;
	}
	.home-swiper {
		/* padding: 29rpx; */
	}
	/* 去除scrollbar底部的滚动条 */
	/deep/ ::-webkit-scrollbar {
		display: none;
	}
</style>